package com.diy.sigmund.diycommon.designpatterns.state;

/**
 * <br>没有25分钱 有25分钱 售出糖果 糖果售罄
 * <br>  投入25分钱  转动曲柄 发放糖果
 * <br>  退回25分钱
 *
 * @author ylm-sigmund
 * @since 2022/2/15 17:31
 */
public class GumballMachine {

    State noQuarterState;
    State hasQuarterState;
    State soldState;
    State soldOutState;
    State winnerState;

    State state = soldOutState;
    int count = 0;

    public GumballMachine(int count) {
        this.count = count;
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        soldOutState = new SoldOutState(this);
        winnerState = new WinnerState(this);
        if (count > 0) {
            state = noQuarterState;
        }
    }

    public void insertQuarterState() {
        state.insertQuarterState();
    }

    public void ejectQuarterState() {
        state.ejectQuarterState();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    public void releaseBall() {
        System.out.println("糖果从槽中被取了出来");
        if (count > 0) {
            count = count - 1;
        }
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public State getState() {
        return state;
    }

    public int getCount() {
        return count;
    }

    public void setState(State state) {
        this.state = state;
    }

}
